<template>
	<div class="page-modal">
		<el-dialog v-model="dialogVisible" title="新建用户" width="30%" center>
			<HyForm v-bind="modalConfig" v-model="formData" ref="hyForm"></HyForm>
      <slot></slot>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="handleConfirmClick"
						>确定</el-button
					>
				</span>
			</template>

		</el-dialog>
	</div>
</template>

<script lang="ts">
	import HyForm from '@/base-ui/form'
	import { defineComponent, ref ,watch} from 'vue'
  import { useStore } from 'vuex'
	export default defineComponent({
		props: {
			modalConfig: {
				type: Object,
				default: () => ({})
			},
      defaultInfo:{
        type:Object,
        default:()=>({})
      },
      pageName:{
        type:String
      },
      otherInfo:{
        type:Object,
        default:()=>({})
      }
		},
		components: {
			HyForm
		},
		setup(props) {
      const store = useStore()
			const dialogVisible = ref(false)
			const formData = ref<any>({})
      watch(()=>props.defaultInfo,(newValue)=>{
        for(const item of props.modalConfig.formItems){
          formData.value[item.field] = newValue[item.field]
        }
      })
      const handleConfirmClick = ()=>{
        dialogVisible.value = false
        if(Object.keys(props.defaultInfo).length){
          //编辑
          store.dispatch('systemModule/editPageDataAction',{
            pageName:props.pageName,
            editInfo:{...formData.value,...props.otherInfo},
            id:props.defaultInfo.id
          })
        }else{
          //新建
          store.dispatch('systemModule/createPageDataAction',{
            pageName:props.pageName,
            newInfo:{...formData.value,...props.otherInfo},
          } )
        }
      }
			return {
				dialogVisible,
				formData,
        handleConfirmClick
			}
		}
	})
</script>


<style>
</style>